import { Component,ViewChild  } from '@angular/core';
import { IonicPage, NavController, NavParams , ToastController, ActionSheetController, AlertController, ViewController, Navbar} from 'ionic-angular';
import { UpgradeAppProvider } from "../../providers/upgrade-app/upgrade-app";
import { ApiProvider } from "../../providers/api/api";
/**
 * Generated class for the RandomCallPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-random-call',
  templateUrl: 'random-call.html',
})
export class RandomCallPage {
  @ViewChild(Navbar) navBar: Navbar;
  begincall: boolean = true;
  timer;
  pic;
  length;
  classname;
  startask = "开始随机提问";
  courseclassid;
  unitId;
  picshow=false;//判断什么时候显示图片
  classcourseId;

  constructor(public navCtrl: NavController, public navParams: NavParams, private wsService: UpgradeAppProvider,public http:ApiProvider) {
  }
  ionViewDidEnter() {
    this.courseclassid = this.navParams.get("courseclassid");

    this.unitId = this.navParams.get("unitId");
  }

  ngAfterViewInit() {
    //this.httpreq._get('',(data)=>console.log(data));
    this.wsService.createObservableSocket(`${this.http.sendwsinfo}/FourInOneTeacherMgt/websocket`)
      .subscribe(
        data => console.log("25" + data),
        err => console.log(err),
        () => console.log('l it s over')
      )

    this.wsService.creatsocket(`${this.http.receiveserve}/FourInOneTeacherAppServer/websocket`)//ws://192.168.0.35:8081/FourInOneTeacherAppServer/websocket
      .subscribe(
        data => this.data_sta(data),
        err => console.log(err),
        () => console.log('it s over')
      )
      this.navBar.backButtonClick
      = this.backButtonClick;
  }


 //子集退回关闭随机提问
  // backButtonClick () {
  //   this.wsService.sendMessage(`closeQuestion,${this.courseclassid},${this.unitId}`);
  // }
  backButtonClick = (e: UIEvent) => {
    this.navCtrl.pop();
    this.wsService.sendMessage(`closeQuestion,${this.courseclassid},${this.unitId}`);
  };

  data_sta(a) {
    if (a.split(',')[0] == "selectStuHead") {
      this.pic = a.split(',')[4];
      this.classname = a.split(',')[5];
      this.picshow=true;
    }
    // console.log(989);
    // console.log(a);
    // if (a.split(',')[0] == "updateStartCourseState") {
    //   if (a.split(',')[4] == 0) {
    //     this.teaching_status = "结束授课";
    //   }
    //   if (a.split(',')[4] == 1) {
    //     this.navCtrl.push(MainPage)
    //   }
    // }

    // if (a.split(',')[0] == "deadLine") {
    //   if (a.split(',')[4]) {
    //     this.ptime = a.split(',')[4]
    //   }
    // }
  }

  begincallb() {
    var sta;
    if (this.startask == "开始随机提问") {
      this.startask = "暂停随机提问";
      sta = "startQuestion";
      this.picshow=false;
    } else {
      this.startask = "开始随机提问";
      sta = "endQuestion";

    }
    this.wsService.sendMessage(`${sta},${this.courseclassid},${this.unitId}`);
  }

}
